

package com.bsh.erp.base;

import com.alibaba.fastjson.JSONObject;
import com.bsh.erp.entity.User;
import com.bsh.erp.service.BusinessBillExtService;
import com.bsh.erp.service.BusinessBillService;
import com.bsh.erp.service.DepartmentService;
import com.bsh.erp.service.EstablishmentTypeService;
import com.bsh.erp.service.InBillService;
import com.bsh.erp.service.OutBillService;
import com.bsh.erp.service.StafferService;
import com.bsh.erp.service.UserService;
import com.bsh.erp.vto.service.StafferVTOService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.ValueStack;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.ParameterizedType;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.portlet.bind.annotation.ResourceMapping;

/**
 * BaseAction  
 * 所有Action都必须继承BaseAction
 * 改Action 包含常用的操作
 * 同时注入所有Service
 * 继承ActionSupport
 * 实现 ModelDriven 模型驱动接口
 * @author JiaQi
 *
 */
public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T>{
//	模型驱动
	protected T model;
	/**
	 *获取模型驱动
	 */
	public T getModel(){
		return model;
	}
	
//	构造方法 通过反射获取实体类对象
	public BaseAction(){
		try{
//			通过反射获取实体类对象
			ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
			Class<T> clazz = (Class<T>) pt.getActualTypeArguments()[0];
			model = clazz.newInstance();
		}catch(Exception e){
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 	分页算法
	 */
	protected int pageNum = 1;//	当前页
	public int getPageNum() {
		return pageNum;
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	
	/**
	 * 获取当前登录用户
	 */
	public User getAdmin(){
		return (User) ActionContext.getContext().getSession().get("admin");
	}
	/**
	 * 放置登录用户
	 */
	public void setAdmin(User user){
		ActionContext.getContext().getSession().put("admin", user);
	}
	
	
	/**
	 * 获取session
	 */
	public Map<String, Object> getActionSession(){
		return ActionContext.getContext().getSession();
	}
	/**
	 * 获取值栈
	 */
	public ValueStack getActionValueStack(){
		return ActionContext.getContext().getValueStack();
	}
	
	/**
	 * 放置提示信息
	 */
	public void setMessage(String msg){
		getActionValueStack().set("msg", msg);
	}
	
	/**
	 * 放置当前账套
	 */
	public void setAccount(String account){
		getActionSession().put("account", account);
	}
	/**
	 * 获取当前账套
	 */
	public String getAccount(){
		return (String) getActionSession().get("account");
	}
	
	/**
	 * 传递接口JSON数据
	 * @author JiaQi
	 * @param jsonString
	 */
	public void outJson(String jsonString){
	    HttpServletResponse response = ServletActionContext.getResponse();
	    /*
	     * 在调用getWriter之前未设置编码(既调用setContentType或者setCharacterEncoding方法设置编码),
	     * HttpServletResponse则会返回一个用默认的编码(既ISO-8859-1)编码的PrintWriter实例。这样就会
	     * 造成中文乱码。而且设置编码时必须在调用getWriter之前设置,不然是无效的。
	     * */
	    response.setContentType("text/html;charset=utf-8");
	    //response.setCharacterEncoding("UTF-8");
	    PrintWriter out;
		try {
		out = response.getWriter();
	    //JSON在传递过程中是普通字符串形式传递的，这里简单拼接一个做测试
	    out.println(jsonString);
	    out.flush();
	    out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
//	注入service
	@Autowired
	protected UserService userService;
	@Autowired
	protected StafferService stafferService;
	@Autowired
	protected StafferVTOService stafferVTOService;
	@Autowired
	protected DepartmentService departmentService;
	@Autowired
	protected EstablishmentTypeService establishmentTypeService;
	@Autowired
	protected InBillService inBillService;
	@Autowired
	protected OutBillService outBillService;
	@Autowired
	protected BusinessBillService businessBillService;
	@Autowired
	protected BusinessBillExtService businessBillExtService;
}
